﻿using CuaHangLego.Bussiness;
using CuaHangLego.Bussiness.Parser;
using CuaHangLego.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CuaHangLego.DataAccess
{
    public class HoaDonBanDA : DABase, IDA<HoaDonBan>
    {
        public static HoaDonBanDA Instance = new HoaDonBanDA();

        public int Insert(HoaDonBan item)
        {
            SqlCommand sql = new SqlCommand();
            sql.CommandText = string.Format("INSERT INTO HOADONBAN ({0},{1},{2}) VALUES('{3}',{4},{5}); SELECT CAST(scope_identity() AS int);",
                "ThoiGian", "KhachHangID", "DaThanhToan",
                TConvert.CSDateTimeToSQL(item.ThoiGian), item.KhachHang.MaSo, item.DaThanhToan ? 1 : 0);
            this.OpenDB();
            int rs = int.Parse(ExecuteQuerySingleValue(sql).ToString());
            this.CloseDB();

            if (rs >= 0)
            {
                foreach (CTHD ct in item.DsCTHD)
                {
                    if (ct.SoLuong > 0) CTHDBanDA.Instance.InsertToHoaDon(rs, ct);
                    if (item.DaThanhToan)
                    {
                        SanPhamKhoDA.Instance.ChangeQuatity(ct.SanPham.MaSo, (-1) * ct.SoLuong);
                    }
                }
            }

            return rs;
        }

        public bool Update(HoaDonBan item)
        {
            SqlCommand sql = new SqlCommand();
            sql.CommandText = string.Format("UPDATE HOADONBAN SET {0}={1} WHERE ID={2};",
                "DaThanhToan",
                item.DaThanhToan ? 1 : 0, item.MaSo);
            OpenDB();
            int rs = ExecuteQuery(sql);
            foreach (CTHD ct in item.DsCTHD)
            {
                SanPhamKhoDA.Instance.ChangeQuatity(ct.SanPham.MaSo, item.DaThanhToan ? (-1) * ct.SoLuong : ct.SoLuong);
                NhatKySanPhamDA.Instance.Insert(ct.SanPham.MaSo);
            }
            CloseDB();
            return rs > 0;
        }

        public bool Delete(HoaDonBan item)
        {
            SqlCommand sql = new SqlCommand();
            sql.CommandText = string.Format("DELETE FROM HOADONBAN WHERE {0}={1}", "ID", item.MaSo);
            OpenDB();
            int rs = ExecuteQuery(sql);
            CloseDB();
            return rs > 0;
        }

        public IList<HoaDonBan> GetAll()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = string.Format("SELECT * FROM HOADONBAN");
            cmd.CommandType = System.Data.CommandType.Text;
            OpenDB();
            SqlDataReader reader = ExecuteQueryReader(cmd);
            IList<HoaDonBan> list = new List<HoaDonBan>();
            while (reader.Read())
            {
                list.Add(HoaDonBanParser.Parse(reader));
            }
            CloseDB();

            // Don't run for lazy load
            
            //foreach (HoaDon hd in list)
            //{
            //    hd.DsCTHD = CTHDBanDA.Instance.GetAllFromHD(hd.MaSo);
            //}

            return list;
        }

        public IList<HoaDonBan> GetAllByKhachHangID(int ID)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = string.Format("SELECT * FROM HOADONBAN WHERE {0}={1}", "KhachHangID", ID);
            cmd.CommandType = System.Data.CommandType.Text;
            OpenDB();
            SqlDataReader reader = ExecuteQueryReader(cmd);
            IList<HoaDonBan> list = new List<HoaDonBan>();
            while (reader.Read())
            {
                list.Add(HoaDonBanParser.Parse(reader));
            }
            CloseDB();

            foreach (HoaDon hd in list)
            {
                hd.DsCTHD = CTHDBanDA.Instance.GetAllFromHD(hd.MaSo);
            }

            return list;
        }

        public HoaDonBan GetByID(int ID)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = string.Format("SELECT * FROM HOADONBAN WHERE {0}={1}", "ID", ID);
            cmd.CommandType = System.Data.CommandType.Text;
            OpenDB();
            SqlDataReader reader = ExecuteQueryReader(cmd);
            HoaDonBan hd = null;
            
            while (reader.Read())
            {
                hd = HoaDonBanParser.Parse(reader);
                break;
            }
            
            if (hd != null) hd.DsCTHD = CTHDMuaDA.Instance.GetAllFromHD(hd.MaSo);
            
            CloseDB();
            return hd;
        }
    }
}
